package com.atguigu.msm.controller;


import com.atguigu.common.Result;

import com.atguigu.msm.entity.UcenterMember;
import com.atguigu.msm.service.UcenterMemberService;
import com.atguigu.msm.vo.RegisterVo;
import com.atguigu.utils.JwtUtils;
import com.atguigu.vo.UcenterMemberVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Summer-day
 * @since 2020-07-30
 */
@RestController
@RequestMapping("/eduMsm/user")
@CrossOrigin
public class UserCenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        return Result.ok().data("token",token);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return Result.ok();
    }

    /**
     * 根据token获取用户信息
     */
    @GetMapping("/getInfo")
    public Result getInfo(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(id);
        return Result.ok().data("info",member);
    }

    /**
     * 根据id获取用户信息
     * @param id 用户id
     */
    @GetMapping("/{id}")
    public UcenterMemberVo getInfoById(@PathVariable String id){
        //返回common包中的对象,因为远程调用,需要统一
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        UcenterMember member = memberService.getById(id);
        BeanUtils.copyProperties(member,ucenterMemberVo);
        return ucenterMemberVo;

    }

    @ApiOperation("查询某一天的注册人数")
    @GetMapping("/registerNum/{date}")
    public Result countRegisterNum(@PathVariable String date){
        Integer count = memberService.countRegisterNum(date);
        return Result.ok().data("registerNum",count);
    }

}

